21. 交叉熵 2-损失函数

交叉熵

我们遇到了某种规律,概率和误差函数之间肯定有一定的联系,这种联系叫做交叉熵。这个概念在很多领域都非常流行,包括机器学习领域。我们将详细了解该公式,并编写代码!

交叉熵 2

交叉熵 3

练习:编写交叉熵

现在该你来发挥作用了!我们用 Python 编写交叉熵公式。

Start Quiz:

import numpy as np

# Write a function that takes as input two lists Y, P,
# and returns the float corresponding to their cross-entropy.
def cross_entropy(Y, P):
    pass
import numpy as np

def cross_entropy(Y, P):
    Y = np.float_(Y)
    P = np.float_(P)
    return -np.sum(Y * np.log(P) + (1 - Y) * np.log(1 - P))